package com.hyt.it.ogt.kq.main.controller.gov;

import com.alibaba.fastjson.JSON;
import com.hyt.common.apiInfor.ApiResponse;
import com.hyt.it.ogt.kq.common.config.ConfigManager;
import com.hyt.it.ogt.kq.common.gov.constant.ResponseCode;
import com.hyt.it.ogt.kq.main.controller.base.BaseController;
import com.hyt.it.ogt.kq.service.gov.model.vo.zjw.InsertExamStudentScoreRequestVO;
import com.hyt.it.ogt.kq.service.gov.service.IZjwService;
import com.hyt.loginfo.model.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiOperationSort;
import io.swagger.annotations.ApiSort;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Objects;

@Api(tags = "137.住建委接口", value = "住建委接口")
@ApiSort(value = 137)
@RestController
@RequestMapping("/kw/gov/zjw")
@Slf4j
public class ZjwController extends BaseController {

    @Resource
    private ConfigManager configManager;
    @Resource
    private IZjwService iZjwService;
    @Resource
    private RedisTemplate redisTemplate;

    @ApiOperation(
            value = "37.1 拉取住建委考试数据",
            notes = "43370101:拉取住建委考试数据异常;43370102:秘钥错误"
    )
    @ApiOperationSort(value = 1)
    @GetMapping("/getExamData")
    public ApiResponse<Object> getExamData(@RequestParam String userKey, @RequestParam String gwfl) {
        try {
            redisTemplate.opsForValue().set("zjw","0");
            User user = getUser();
            if (Objects.isNull(user) || Objects.isNull(user.getId())) {
                return ApiResponse.builder()
                        .code(ResponseCode.USER_NOT_LOGIN.getCode())
                        .build();
            }
            if(!userKey.equals(configManager.getZjwUserKey())) {
                return ApiResponse.builder()
                        .code(43370102)
                        .build();
            }
            iZjwService.getExamData(gwfl, user);
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .build();
        } catch (Exception e) {
            log.error(StringUtils.join(43370101, ": # 拉取住建委考试数据数据异常"), e);
            return ApiResponse.builder()
                    .code(43370101)
                    .build();
        }
        finally {
            redisTemplate.opsForValue().set("zjw","100");
        }
    }

    @ApiOperation(
            value = "37.2 推送住建委考试成绩数据",
            notes = "43370201:拉取住建委考试数据异常;"
    )
    @ApiOperationSort(value = 2)
    @PostMapping("/pushCjToZjw")
    public ApiResponse<Object> pushCjToZjw(@RequestParam String userKey, @RequestBody InsertExamStudentScoreRequestVO insertExamStudentScoreRequest) {
        try {
            log.info("推送住建委成绩请求参数：【{}】", JSON.toJSONString(insertExamStudentScoreRequest));
            if(!userKey.equals(configManager.getZjwUserKey())) {
                return ApiResponse.builder()
                        .code(43370102)
                        .data(false)
                        .build();
            }
            return ApiResponse.builder()
                    .code(ResponseCode.SUCCESS.getCode())
                    .data(iZjwService.pushCjToZjw(insertExamStudentScoreRequest))
                    .build();
        } catch (Exception e) {
            log.error(StringUtils.join(43370201, ": # 推送住建委考试成绩数据异常"), e);
            return ApiResponse.builder()
                    .code(43370201)
                    .data(false)
                    .build();
        }
    }

    @ApiOperation(
            value = "37.3 拉取住建委考试数据进度",
            notes = "43370101:拉取住建委考试数据异常;43370102:秘钥错误"
    )
    @ApiOperationSort(value = 3)
    @GetMapping("/getZjwProgess")
    public ApiResponse<Object> getZjwProgess() {
        return ApiResponse.builder()
                .code(ResponseCode.SUCCESS.getCode())
                .data(iZjwService.getZjwProgess())
                .build();
    }

}
